﻿using LoveEatRabbit.API.Write.Applications.Commands.Permissions;
using LoveEatRabbit.API.Write.Applications.Commands.Roles;
using LoveEatRabbit.API.Write.Applications.Commands.Users;
using LoveEatRabbit.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace LoveEatRabbit.API.Write.Controllers
{
    /// <summary>
    /// API控制器—写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public ManagementController(ILogger<ManagementController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 权限添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddPermission(PermissionCreatedCommand command)
        {
            try
            {
                logger.LogInformation("权限添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("权限添加失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 权限逻辑删除
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteRepmission(DeletePerCommand command)
        {
            try
            {
                logger.LogInformation("逻辑删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("逻辑删除失败" + ex.Message);
                throw;

            }

        }
        /// <summary>
        /// 权限修改方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdatePer(UpdatePermissionCommand command)
        {
            try
            {
                logger.LogInformation("修改成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("修改失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 用户添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddUser(UserCreatedCommand command)
        {
            try
            {
                logger.LogInformation("用户添加成功");
                return await mediator.Send(command);    
            }
            catch (Exception ex)
            {
                logger.LogError("用户添加失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 用户删除方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteUser(UserDeleteCommand command)
        {
            try
            {
                logger.LogInformation("用户删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("用户删除失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 用户修改方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateUser(UserUpdateCommand command)
        {
            try
            {
                logger.LogInformation("用户修改成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("用户修改失败" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 角色删除方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public async Task<APIResult<int>> DeleteRole(DeleteRoleCommand command)
        {
            try
            {
                logger.LogInformation("角色删除成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色删除失败"+ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 角色添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddRoleRange(AddRoleCommand command)
        {
            try
            {
                logger.LogInformation("角色添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色添加失败" + ex.Message);
                throw;
            }
        }


        /// <summary>
        /// 角色分配权限修改方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> RoleUpdate(UpdateRoleCommand command)
        {
            try
            {
                logger.LogInformation("角色分配权限修改成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("角色分配权限修改失败");
                throw;
            }
        }

    }
}
